﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 三国杀
{
    public partial class ModifyId: Form
    {
        public ModifyId()
        {
            InitializeComponent();
        }

        public string Userid;
        public string password;

        //初始化
        private void Init()
        {
            if (string.IsNullOrEmpty(this.Userid)||string.IsNullOrEmpty(this.password))
            {
                MessageBox.Show("变量未正确赋值，无法进行查询！");
                return;
            }

            DB db = new DB();
            try
            {
                db.dbOpen();
                string sql = "SELECT * FROM QQUsers WHERE QQ = @QQ AND Pwd = @Password";
                SqlCommand comm = new SqlCommand(sql, db.Conn);
                comm.Parameters.AddWithValue("@QQ", Userid);
                comm.Parameters.AddWithValue("@Password", password);
                SqlDataReader read = comm.ExecuteReader();
                while (read.Read())
                {
                    textBox_name.Text = read["Name"].ToString();
                    textBox_phone.Text = read["Phone"].ToString();
                    textBox_sig.Text = read["qianming"].ToString();
                    textBox_sex.Text = read["sex"].ToString();
                    textBox_birth.Text = read["time"].ToString();
                    textBox_email.Text = read["youxiang"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询或填充数据时出错: {ex.Message}");
                Console.WriteLine($"详细错误: {ex.ToString()}");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Init();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DB db = new DB();//数据库对象
            db.dbOpen();

            // 首先检查两次密码是否一致
            if (textBox_pwd1.Text != textBox_pwd2.Text)
            {
                MessageBox.Show("两次密码输入不一致，请重新输入！");
                db.dbClose();
                return; // 直接返回，不执行后续代码
            }

            // 查询数据库判断输入账号是否存在
            string checkSql = "SELECT COUNT(*) FROM QQUsers WHERE QQ = @inputQQ";
            SqlCommand checkComm = new SqlCommand(checkSql, db.Conn);
            checkComm.Parameters.AddWithValue("@inputQQ", textBox_QQ.Text);
            int count = (int)checkComm.ExecuteScalar();
            if (count == 0)
            {
                MessageBox.Show("输入的账号不正确，请重新输入！");
                db.dbClose();
                return;
            }

            string updateSql = "UPDATE QQUsers " + "SET Name = @name, Phone = @phone," +
                " youxiang = @email, time = @birth, sex= @sex , qianming = @qianming , Pwd = @Pwd " +
                "WHERE QQ = @QQ";
            SqlCommand updateCommand = new SqlCommand(updateSql, db.Conn);

            // 为参数赋值
            updateCommand.Parameters.AddWithValue("@sex", textBox_sex.Text);
            updateCommand.Parameters.AddWithValue("@qianming", textBox_sig.Text);
            updateCommand.Parameters.AddWithValue("@name", textBox_name.Text);
            updateCommand.Parameters.AddWithValue("@email", textBox_email.Text);
            updateCommand.Parameters.AddWithValue("@phone", textBox_phone.Text);
            updateCommand.Parameters.AddWithValue("@birth", textBox_birth.Text);  
            updateCommand.Parameters.AddWithValue("@QQ", textBox_QQ.Text);
            if (textBox_pwd2.Text != ""&& textBox_pwd1.Text != "")
            {
                updateCommand.Parameters.AddWithValue("@Pwd", textBox_pwd2.Text);
            }
            else
            {
                updateCommand.Parameters.AddWithValue("@Pwd", password);
            }

            int rowsAffected = updateCommand.ExecuteNonQuery();
            db.dbClose();

            if (rowsAffected > 0)
            {
                MessageBox.Show("数据更新成功！");

                // 更新成功后关闭当前窗口
                this.DialogResult = DialogResult.OK; // 或者 DialogResult.Yes
                this.Close();
            }
            else
            {
                MessageBox.Show("未找到匹配的记录，更新失败！");
            }
        }

      
    }
}
